package com.photoeditor.demo.ui.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;

import me.xiaopan.sketch.SketchImageView;

/**
 * Created by edward on 17-10-24.
 */

public class RoundSketchImageView extends SketchImageView {
    private Path mPath;
    private int mRadius;
    private RectF mRectF;
    private Paint mPaint;

    public RoundSketchImageView(Context context) {
        this(context, null);
    }

    public RoundSketchImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
    }

    public void setRadius(int radius) {
        mRadius = radius;
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.save();
        if (mPath != null) {
            canvas.clipPath(mPath);
        }
        super.draw(canvas);
        canvas.restore();
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (mRadius != 0) {
            if (mPath == null) {
                mPath = new Path();
                mRectF = new RectF();
            }
            mPath.reset();
            mRectF.set(0, 0, getWidth(), getHeight());
            mPath.addRoundRect(mRectF, mRadius, mRadius, Path.Direction.CW);
        }
    }
}
